//
//  XCHomeVC.m
//  XCNewsApp
//
//  Created by UJY on 2016/12/7.
//  Copyright © 2016年 XC. All rights reserved.
//

#import "XCHomeVC.h"
#import "XCListBar.h"
#import "XCArrow.h"
#import "XCDetailsList.h"
#import "XCDeleteBar.h"
#import "XCScroller.h"
#import "XCCollectionViewCell.h"



#define kListBarH 30
#define kArrowW 40
#define kAnimationTime 0.8

@interface XCHomeVC () <UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) XCListBar *listBar;

@property (nonatomic,strong) XCDeleteBar *deleteBar;

@property (nonatomic,strong) XCDetailsList *detailsList;

@property (nonatomic,strong) XCArrow *arrow;

@property(strong,nonatomic) UICollectionView *collectionView;

@end

@implementation XCHomeVC

static NSString *const ID= @"cell";
//static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNaviBar];
    
    [self makeContent];
    
}

-(void)setupNaviBar
{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTranslucent:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"title"]];
    CGRect frame = imageView.frame;
    frame.origin.x = 20;
    frame.origin.y = 12;
    imageView.frame = frame;
    [self.navigationController.navigationBar addSubview:imageView];
    CGFloat textFx = 40 + imageView.frame.size.width;
    CGFloat textFw = [UIScreen mainScreen].bounds.size.width - textFx - 20;
    UISearchBar *sea = [[UISearchBar alloc]init];
    sea.frame = CGRectMake(textFx, 7, textFw, 30);
    sea.placeholder = @"搜索你想搜索的";
    [self.navigationController.navigationBar addSubview:sea];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:UIBarMetricsDefault];
}


-(void)makeContent
{
    NSMutableArray *listTop = [[NSMutableArray alloc] initWithArray:@[@"推荐",@"热点",@"杭州",@"社会",@"娱乐",@"科技",@"汽车",@"体育",@"订阅",@"财经",@"军事",@"国际",@"正能量",@"段子",@"趣图",@"美女",@"健康",@"教育",@"特卖",@"彩票",@"辟谣"]];
    NSMutableArray *listBottom = [[NSMutableArray alloc] initWithArray:@[@"电影",@"数码",@"时尚",@"奇葩",@"游戏",@"旅游",@"育儿",@"减肥",@"养生",@"美食",@"政务",@"历史",@"探索",@"故事",@"美文",@"情感",@"语录",@"美图",@"房产",@"家居",@"搞笑",@"星座",@"文化",@"毕业生",@"视频"]];
    
    __weak typeof(self) unself = self;
    
    if (!self.detailsList) {
        NSLog(@"%lf",(kListBarH - kScreenH));
        self.detailsList = [[XCDetailsList alloc] initWithFrame:CGRectMake(0, kListBarH - kScreenH, kScreenW, kScreenH - kListBarH)];
        self.detailsList.listAll = [NSMutableArray arrayWithObjects:listTop,listBottom, nil];
        self.detailsList.longPressedBlock = ^(){
            [unself.deleteBar sortBtnClick:unself.deleteBar.sortBtn];
        };
        self.detailsList.opertionFromItemBlock = ^(animateType type, NSString *itemName, int index){
            [unself.listBar operationFromBlock:type itemName:itemName index:index];
        };
        [self.view addSubview:self.detailsList];
    }
    
    if (!self.listBar) {
        self.listBar = [[XCListBar alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kListBarH)];
        self.listBar.visibleItemList = listTop;
        self.listBar.arrowChange = ^(){
            if (unself.arrow.arrowBtnClick) {
                unself.arrow.arrowBtnClick();
            }
        };
        self.listBar.listBarItemClickBlock = ^(NSString *itemName , NSInteger itemIndex){
            [unself.detailsList itemRespondFromListBarClickWithItemName:itemName];
            
            //移动到该位置
            unself.collectionView.contentOffset =  CGPointMake(itemIndex * unself.collectionView.frame.size.width, 0);
            [unself.collectionView reloadData];
        };
        [self.view addSubview:self.listBar];
    }
    
    if (!self.deleteBar) {
        self.deleteBar = [[XCDeleteBar alloc] initWithFrame:self.listBar.frame];
        [self.view addSubview:self.deleteBar];
    }
    
    
    if (!self.arrow) {
        self.arrow = [[XCArrow alloc] initWithFrame:CGRectMake(kScreenW - kArrowW, 0, kArrowW, kListBarH)];
        self.arrow.arrowBtnClick = ^(){
            unself.deleteBar.hidden = !unself.deleteBar.hidden;
            [UIView animateWithDuration:kAnimationTime animations:^{
                CGAffineTransform rotation = unself.arrow.imageView.transform;
                unself.arrow.imageView.transform = CGAffineTransformRotate(rotation,M_PI);
                unself.detailsList.transform = (unself.detailsList.frame.origin.y<0)?CGAffineTransformMakeTranslation(0, kScreenH):CGAffineTransformMakeTranslation(0, -kScreenH);
            }];
        };
        [self.view addSubview:self.arrow];
    }
    
    
    if (!self.collectionView) {
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        flowLayout.minimumLineSpacing=0;
        flowLayout.minimumInteritemSpacing=0;
        self.collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, kListBarH, kScreenW , kScreenH - kListBarH - 64 - 49) collectionViewLayout:flowLayout];
        self.collectionView.dataSource=self;
        self.collectionView.delegate=self;
        self.collectionView.pagingEnabled = YES;
        [self.collectionView setBackgroundColor:[UIColor whiteColor]];
        self.collectionView.showsHorizontalScrollIndicator=NO;
        //注册Cell，必须要有
        [self.collectionView registerClass:[XCCollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        
        [self.view insertSubview:self.collectionView atIndex:0];
    }
}


#pragma mark -- UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.detailsList.topView.count;
}


//每个UICollectionView展示的内容
-(XCCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"UICollectionViewCell";
    XCCollectionViewCell * cell = (XCCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:1.0f];
    
    UITableView *tableView = [[UITableView alloc]init];
    
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    cell.tableView = tableView;
    
    
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kScreenW , kScreenH - kListBarH - 64 - 49);
}


#pragma mark --UICollectionViewDelegate

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    [self.listBar itemClickByScrollerWithIndex:scrollView.contentOffset.x / self.collectionView.frame.size.width];
}




@end
